<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form
      ref="fm"
      label-width="100px"
      :model="userDetail"
      :rules="rules"
    >
      <!-- 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item
            label="部门"
            prop="departmentName"
          >
            <el-input v-model="userDetail.departmentName" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item
            label="入职时间"
            prop="correctionTime"
          >
            <el-date-picker
              v-model="userDetail.correctionTime"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <UploadImg :staff-photo.sync="userDetail.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row
        class="inline-info"
        type="flex"
        justify="center"
      >
        <el-col :span="12">
          <el-button
            type="primary"
            @click="updateUser"
          >保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { saveUserDetailById } from '@/api/employees.js'
export default {
  props: {
    /**
     * 单向数据流（定义：数据的传递和修改都是单向的） 场景：父子通信
     * 注意：
     * 父组件传递给子组件的数据正常来说只能在父组件修改
     * 1、父传子是简单类型，只能在父组件修改
     * 2、父传子是复杂类型，可以在子组件局部修改，但是不能改地址
    */
    userDetail: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      rules: {
        departmentName: [{ required: true, message: '部门名必填！', trigger: 'change' }],
        correctionTime: [{ required: true, message: '时间必填！', trigger: 'change' }]
      }
    }
  },
  methods: {
    async updateUser () {
      try {
        await this.$refs.fm.validate()
        await saveUserDetailById(this.userDetail)
        this.$message.success('更新成功')
        this.$router.back()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
